import 'package:flutter/material.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBEngineer/YBBaseRouter/YBApplication.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBColor/YBColor.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBTextStyle/YBTextStyle.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBLoginModule/YBLoginSubPage/YBLoginHeaderInfoPage.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBLoginModule/YBLoginSubPage/YBLoginSubPage.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBNetWorkModule/YBLoginInfoModule/YBLoginInfoApi.dart';

import 'YBLoginProvider/YBEventBus.dart';


class YBLoginBangDingPhonePage extends StatefulWidget {
  @override
  _YBLoginBangDingPhonePageState createState() => _YBLoginBangDingPhonePageState();
}

class _YBLoginBangDingPhonePageState extends State<YBLoginBangDingPhonePage> {

  String _phoneNumber;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    eventBus.on<YBPhoneEventBus>().listen((event) {
      print('----${event.phoneNumber}');
      _phoneNumber = event.phoneNumber;
    });

  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Scaffold(
        appBar: AppBar(
          elevation: 0,
          title:Text('绑定手机号'),
        ),
        body: SingleChildScrollView(
          child: Padding(
            padding: EdgeInsets.only(left: 20,right:20,bottom: 10,top: 10),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                Container(
                  child: YBLoginHeaderInfoPage(),
                ),
                Container(
                  height: 20,
                ),
                Container(
                  child: Text("绑定手机号",style: YBTextStyle.login1C1D1F28SemiBold,),
                ),
                Container(
                  height: 20,
                ),
                Container(
                  child: YBLoginSubPage(),
                ),
                Container(
                  height: 20,
                ),
                Container(
                  height: 44,
                  child: RawMaterialButton(
                      child: Text("登录",style:YBTextStyle.xieGangQingNianTuiGuangYuanBtnText,),
                      fillColor: YBColor.commonThemeColor,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(Radius.circular(22.0)),
                      ),
                      onPressed:(){
                        _initLogin(_phoneNumber, context);
                      }
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  void _initLogin(String phoneNumber,BuildContext context){
    YBLoginInfoApi.loginInMsgCode({"phone":phoneNumber,'sysCode':"xg_youbang"},success:(data){
      print('----$data----');
      var msgTokenMap = data;
      //msgToken
      print('msgToken---$msgTokenMap');
      if (msgTokenMap['msgToken'].toString().isNotEmpty) {
        YBApplication.router.navigateTo(context, '/loginIn/loginYanZhengMaPage?msgToken=${msgTokenMap['msgToken']}&&phoneN=$phoneNumber&&type=2');
      }
    },fail: (error){
      print("---$error---");
    });
  }

}
